<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'

const schema = {
    type: 'page',
    body: {
      type: 'crud',
      id: 'curd',
      api: '/base/dict/list',
      syncLocation: false,
      autoFillHeight: true,
      columnsTogglable: false,
      footerToolbar: [
        'statistics',
        'switch-per-page',
        'pagination'
      ],
      autoGenerateFilter: {
        showBtnToolbar: false,
        columnsNum: 3
      },
      bulkActions: [
        {
          label: '批量删除',
          actionType: 'ajax',
          api: {
            method: 'post',
            url: '/base/dict/deleteBatch',
            data:{
              ids: '${ids}'
            }
          },
          confirmText: '确定要批量删除?'
        }
      ],
      headerToolbar:[
        'bulkActions',
        {
          type: 'button',
          label: '刷新缓存',
          actionType: 'ajax',
          api: '/base/dict/cache/refresh',
          align: 'right',
        },
        {
          type: 'button',
          label: '批量新增',
          align: 'right',
          actionType: 'dialog',
          dialog: {
            title: '批量新增字典值',
            closeOnEsc: true,
            showErrorMsg: false,
            size: 'md',
            body: {
              type: 'form',
              api: '/base/dict/addBatch',
              messages: {
                //不展示表单失败信息
                validateFailed: ""
              },
              body:{
                type: 'textarea',
                label: '字段值',
                name: 'content',
                minRows: 5,
                maxLength: 1000,
                required: true,
                validationErrors: {
                  isRequired: '值不能为空'
                }
              }
            }
          }
        },
        {
          type: 'button',
          actionType: 'dialog',
          align: 'right',
          label: '新增',
          icon: 'fa fa-plus pull-left',
          primary: true,
          dialog: {
            title: '新增字典值',
            closeOnEsc: true,
            showErrorMsg: false,
            body: {
              type: 'form',
              name: 'dict-add-form',
              api: 'post:/base/dict/add',
              canAccessSuperData: false,
              messages: {
                //不展示表单失败信息
                validateFailed: ""
              },
              body: [
                {
                  type: 'input-text',
                  name: 'typeName',
                  label: '类别名称',
                  maxLength: 50,
                },
                {
                  type: 'input-text',
                  name: 'typeCode',
                  label: '类别编码',
                  maxLength: 20,
                  required: true,
                  validationErrors: {
                    isRequired: '请输入类别编码'
                  }
                },
                {
                  type: 'input-text',
                  name: 'itemName',
                  label: '字典名称',
                  maxLength: 50,
                },
                {
                    type: 'input-text',
                    name: 'itemCode',
                    label: '字典编码',
                    maxLength: 20,
                    required: true,
                    validationErrors: {
                      isRequired: '请输入字典编码'
                    }
                },
                {
                    type: 'input-number',
                    name: 'sortNo',
                    label: '排序号',
                    value: '0',
                    max: 999999,
                    min: 0,
                    maxLength: 6,
                }
              ]
            }
          },
          reload:'curd,searchTypeName,searchTypeCode'
        }
      ],
      columns: [
        {
          name: 'typeName',
          label: '类别名称',
          searchable: {
              type: 'input-text',
              name: 'typeName',
              id: 'searchTypeName',
              label: '类别名称',
              source: '/base/dict/list/typeName',
              clearable: true
          }
        },
        {
            name: 'typeCode',
            label: '类别编码',
            searchable: {
                type: 'input-text',
                name: 'typeCode',
                id: 'searchTypeCode',
                label: '类别编码',
                source: '/base/dict/list/typeCode',
                clearable: true
            }
        },
        {
          name: 'itemName',
          label: '字典名称'
        },
        {
            name: 'itemCode',
            label: '字典编码'
        },
        {
            name: 'sortNo',
            label: '排序号',
        },
        {
          type: 'operation',
          label: '操作',
          buttons: [
          {
              label: '修改',
              type: 'button',
              actionType: 'dialog',
              dialog:{
                title: '修改字典',
                closeOnEsc: true,
                showErrorMsg: false,
                body:{
                  type: 'form',
                  api:'post:/base/dict/update',
                  messages:{
                    validateFailed:''
                  },
                  body:[
                    {
                      type: 'hidden',
                      name: 'id',
                    },
                    {
                      type: 'input-text',
                      name: 'typeName',
                      label: '类别名称',
                      maxLength: 50,
                    },
                    {
                        type: 'input-text',
                        name: 'typeCode',
                        label: '类别编码',
                        maxLength: 20,
                        required: true,
                        validationErrors: {
                          isRequired: '请输入类别编码'
                        }
                    },
                    {
                      type: 'input-text',
                      name: 'itemName',
                      label: '字典名称',
                      maxLength: 50,
                    },
                    {
                        type: 'input-text',
                        name: 'itemCode',
                        label: '字典编码',
                        maxLength: 20,
                        required: true,
                        validationErrors: {
                            isRequired: '请输入字典编码'
                        }
                    },
                    {
                        type: 'input-number',
                        name: 'sortNo',
                        label: '排序号',
                        min: 0,
                        max: 999999,
                        maxLength: 6,
                    }
                  ]
                }
              },
              reload:'curd,searchTypeName,searchTypeCode'
            },
            {
              label: '复制',
              type: 'button',
              actionType: 'dialog',
              dialog:{
                title: '复制新增',
                closeOnEsc: true,
                showErrorMsg: false,
                body:{
                  type: 'form',
                  api:'post:/base/dict/add',
                  messages:{
                    validateFailed:''
                  },
                  body:[
                    {
                      type: 'input-text',
                      name: 'typeName',
                      label: '类别名称',
                      maxLength: 50,
                    },
                    {
                        type: 'input-text',
                        name: 'typeCode',
                        label: '类别编码',
                        maxLength: 20,
                        required: true,
                        validationErrors: {
                          isRequired: '请输入类别编码'
                        }
                    },
                    {
                      type: 'input-text',
                      name: 'itemName',
                      label: '字典名称',
                      maxLength: 50,
                    },
                    {
                        type: 'input-text',
                        name: 'itemCode',
                        label: '字典编码',
                        maxLength: 20,
                        required: true,
                        validationErrors: {
                            isRequired: '请输入字典编码'
                        }
                    },
                    {
                        type: 'input-number',
                        name: 'sortNo',
                        label: '排序号',
                        min: 0,
                        max: 999999,
                        maxLength: 6,
                    }
                  ]
                }
              },
              reload:'curd,searchTypeName,searchTypeCode'
            },
            {
              label: '删除',
              type: 'button',
              level: 'danger',
              actionType: 'ajax',
              confirmText: '确认要删除该记录？',
              api: {
                url: '/base/dict/delete?id=${id}',
                method: 'post',
                data:{}
              },
              reload:'curd,searchTypeName,searchTypeCode'
            }
          ]
        }
      ]
    }
  }

</script>

